var sortableActions = { handle: ".moveField",
onUpdate: function(container) { ajaxMove(Sortable.serialize(container)); },
onChange: function(container) {
		id = container.id.split('_');
		if($('edit_'+id[1]).value != '' ) {
			ajaxEdit(id[1]);
		}
	}
};

function getNewList(old) {
	$('list').innerHTML = '';
	var oldvisible = $('info_visible').value;
	$('info_visible').value = '0';
	document.onmousedown = onMouseDown;
	document.onmouseup = onMouseUp;
	setCookie(id, $("info_visible").value );
	if(parseInt(oldvisible) == parseInt(old)) {
		$('more_entries').style.display = 'block';
		isUpdating = true;
		getList(0);
		setTimeout("updatePage()", 0);
	} else {
		isUpdating = true;
		getList(0);
	}
}

function getList (start) {
	var json = '';
	new Ajax.Request('entries.cfm', {
		method:'get',
		parameters: {action: 'getEntriesList', start: start},
		onSuccess: function(transport){
			json = transport.responseText.evalJSON();
			createList(json);
			isUpdating = false;
		},
		onFailure: function() {
			doFailure(true);
			isUpdating = false;
		}
	});
}

function createList (json) {
	var i = 0;
	var j = 0;
	var html = '';
	if(parseInt($('info_visible').value) == 0)
		$('list').innerHTML = '<ul class="days_list" id="masterList"></ul>';
	while (typeof(json[i]) != "undefined") {
		html = "<li class=\"day\" id=\"day_"+json[i].id+"\"><div class=\"day_string\"><img src=\"../data/icons/calendar_view_day.png\"> " + json[i].date;
		html = html + '<input type="hidden" id="visible_fields_'+json[i].id+'" value="0"><input type="hidden" id="school_data_'+json[i].id+'" value="'+json[i].school+'">';
		if(json[i].school == 1)
			html = html + '<img src=\"../data/icons/school.png\" class="school" onclick="toggleSchool('+json[i].id+');" id="school_'+json[i].id+'">';
		else
			html = html + '<img src=\"../data/icons/work.png\" class="school" onclick="toggleSchool('+json[i].id+');" id="school_'+json[i].id+'">';
		html = html + '</div>';
		html = html + '<ul class="entries_list" id="day_entries_' +json[i].id+ '">';
		j = 0;
		while (typeof(json[i].data[j]) != "undefined") {
			element = json[i].data[j];
			html = html + '<li class="entry" id="item_'+element.id+'">';
			html = html + '<div><div class="moveField"></div>';
			html = html + '<div class="entry_text" id="entry_' + element.id + '" onclick="startEdit(' + element.id + ');">' + element.text + '</div>';
			html = html + "<div class=\"entry_delete\" onclick=\"ajaxDelete('"+element.id+"', '"+json[i].id+"');\">X</div></div><div class=\"cleardiv\" /></div></li>";
			j++;
		}
		html = html+'</ul></li>';
		Element.insert('masterList', html);
		Sortable.create('day_entries_' +json[i].id, sortableActions);
		$('visible_fields_'+json[i].id).value = j;
		i++;
	}
	$('info_visible').value = parseInt($('info_visible').value)+i;
	return html;
}

function updatePage(){
	if (isUpdating == false && mouseState == "up" && getPageHeight() - getScrollHeight() < preloadDistance) {
		setCookie(id, $("info_visible").value );
		isUpdating = true;
		getList ($('info_visible').value);
	}
	if (parseInt($("info_visible").value) < parseInt($("info_max").value)) {
		setTimeout("updatePage()", checkInterval);
	} else {
		$('more_entries').style.display = 'none';
	}
}

function onMouseDown(e ){
	mouseState = "down";
	setCookie(id+"_height", getScrollHeight());
	setCookie(id+"_scroll", getScrollHeight() - _getWindowHeight());
}

function onMouseUp(){
	mouseState = "up";
}

var checkInterval = 110;
var preloadDistance = 500;
var isUpdating = false;
var mouseState = "up";
var id;

function init( ){
	document.onmousedown = onMouseDown;
	document.onmouseup = onMouseUp;
	$('info_visible').value = 0;

	setTimeout("updatePage()", 0);
}

Event.observe(window, 'load', init, false);

function setCookie(name, value, expires, path, domain, secure) {
  document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else {
    begin += 2;
  }
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) {
    end = dc.length;
  }
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function getPageHeight() {
  var y;
  var test1 = document.body.scrollHeight;
  var test2 = document.body.offsetHeight
  if (test1 > test2) {
    y = document.body.scrollHeight;
  } else {
	  y = document.body.offsetHeight;
  }
  return parseInt(y);
}

function _getWindowHeight(){
  if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    frameWidth = document.documentElement.clientWidth;
    frameHeight = document.documentElement.clientHeight; 
  } else if (document.body) {
    frameWidth = document.body.clientWidth;
    frameHeight = document.body.clientHeight;
  }
  return parseInt(frameHeight);
}

function getScrollHeight(){
  var y;
  // all except Explorer
  if (self.pageYOffset) {
      y = self.pageYOffset;
  } else if (document.documentElement && document.documentElement.scrollTop) {
      y = document.documentElement.scrollTop;
  } else if (document.body)	{
      y = document.body.scrollTop;
  }
  return parseInt(y)+_getWindowHeight();
}